<? include block('htmla'); 

$ui = array('f' => get_global('ui-adm-mb-f', COM_DEF_FILTER));
$p = $DATA['p'];

startScript('jquery, jquery.sbxa, jquery.tablednd, jquery.form, jquery.jeditable.min'); ?><script type="text/javascript">

jQuery(function($) {
	var d = {l01:<?=$DATA['page']['id'] + (int)param('node')?>};
	var sel = $("#create-form select[name='value']"), descr = $("#create-form .descr");
	
	var uif = <?=$ui['f']?>;
	function uiFilter() {

		$(".object").each(function(){
			var vis = true, obj = $(this);
			if ((uif & <?=COM_ENABLED?>) && obj.find(".button.poweroff").length) vis = false;
			if ((uif & <?=COM_VISIBLE?>) && obj.find(".button.hidden").length) vis = false;
			if ((uif & <?=COM_GLOBAL?>) && obj.find(".button.globalon").length) vis = false;
			if (vis) 
				obj.show();
			else
				obj.hide();
		});
	}
	uiFilter();
	
	sel.change(function () {
		descr.addClass("hidden");
		$("#com-create-" + sel.find("option:selected").attr("value").replace(" ", "-")).removeClass("hidden");
	}).change();
	
	<? if ($p['configure']) { ?>
	$("#objects").tableDnD({
	    onDragClass: "dragging",
	    onDrop: function(table, row) {
			$.post("sort-manual/", {order:$(table).tableDnDSerialize(), l01:d.l01});
	    },
		dragHandle: "dragHandle"
	});
	
	$("#objects .object").hover(function() {
          $(this).addClass('showDragHandle');
	    }, function() {
          $(this).removeClass('showDragHandle');
	    }
	);
	<? } ?>
	
	$("#objects a.button").click(function() {
		var self = $(this), href = self.attr("href"), action = self.attr("class").split(" ")[1];
		var parent = self.parents(".object");

		if (parent.length) { var id = parent ? parent.attr("id").substr(4) : 0, title = self.attr("title").match(/(«.*»)/)[0];

			switch(action){
				
				case "visible":
					$.post(href, d);
					self.removeClass('visible').addClass('hidden').attr("title", "<?=ht('Show', 'tpl')?> " + title).attr("href", href.replace("hide", "show"));
					return false;
		
				case "hidden":
					$.post(href, d);
					self.removeClass('hidden').addClass('visible').attr("title", "<?=ht('Hide', 'tpl')?> " + title).attr("href", href.replace("show", "hide"));
					return false;
		
				case "poweron":
					$.post(href, d);
					self.removeClass('poweron').addClass('poweroff').attr("title", "<?=ht('Enable', 'tpl')?> " + title).attr("href", href.replace("disable", "enable"));
					return false;
		
				case "poweroff":
					$.post(href, d);
					self.removeClass('poweroff').addClass('poweron').attr("title", "<?=ht('Disable', 'tpl')?> " + title).attr("href", href.replace("enable", "disable"));
					return false;
		
				case "globalon":
					$.post(href, d);
					self.removeClass('globalon').addClass('globaloff').attr("title", "<?=ht('Make', 'tpl')?> " + title + " <?=t('global', 'tpl')?>").attr("href", href.replace("localize", "globalize"));
					return false;
		
				case "globaloff":
					$.post(href, d);
					self.removeClass('globaloff').addClass('globalon').attr("title", "<?=ht('Make', 'tpl')?> " + title + " <?=t('local', 'tpl')?>").attr("href", href.replace("globalize", "localize"));
					return false;
		
				case "kill":
					if (confirm('<?=ht('Are you sure you want to kill', 'tpl')?> ' + title + '?')) {
						$.post(href, d);
						parent.remove();
					}
					return false;
		
				case "import":
					var p = self.position(), block = parent.find(".import-block");
					block.find(".diags").hide();
					block.find(".form").show();
					block.css({left: (p.left + 20) + "px", top: (p.top + 6) + "px"}).toggle();
					return false;
			}
			
		} else {	// header buttons
			
			switch(action){
				case "poweron":
					$.post(href, d);
					self.removeClass('poweron').addClass('poweroff').attr("title", "<?=ht('Show unpowered', 'tpl')?>").attr("href", href.replace("stop", "pass"));
					uif |= <?=COM_ENABLED?>;
					break;

				case "poweroff":
					$.post(href, d);
					self.removeClass('poweroff').addClass('poweron').attr("title", "<?=ht('Hide unpowered', 'tpl')?>").attr("href", href.replace("pass", "stop"));
					uif &= ~<?=COM_ENABLED?>;
					break;

				case "visible":
					$.post(href, d);
					self.removeClass('visible').addClass('hidden').attr("title", "<?=ht('Show hidden', 'tpl')?>").attr("href", href.replace("stop", "pass"));
					uif |= <?=COM_VISIBLE?>;
					break;

				case "hidden":
					$.post(href, d);
					self.removeClass('hidden').addClass('visible').attr("title", "<?=ht('Hide hidden', 'tpl')?>").attr("href", href.replace("pass", "stop"));
					uif &= ~<?=COM_VISIBLE?>;
					break;

				case "globalon":
					$.post(href, d);
					self.removeClass('globalon').addClass('globaloff').attr("title", "<?=ht('Show global', 'tpl')?>").attr("href", href.replace("stop", "pass"));
					uif |= <?=COM_GLOBAL?>;
					break;

				case "globaloff":
					$.post(href, d);
					self.removeClass('globaloff').addClass('globalon').attr("title", "<?=ht('Hide global', 'tpl')?>").attr("href", href.replace("pass", "stop"));
					uif &= ~<?=COM_GLOBAL?>;
					break;

				default: return false;
			}
			
			uiFilter();
			return false;
		}


		return true;
	});


	$("input[name='import_objects']").click(function(){
		var self = $(this), parent = self.parents(".object"), id = parent.attr("id").substr(4);
		var form = self.parents("form"), block = self.parents(".import-block");


		form.ajaxSubmit({ dataType: "json", url: 'import-' + id + '/', resetForm: true,
			success: function(data) {	
				if (data.diags) {
					block.find('.form').hide();
					block.find('.diags div').html(data.diags);
					block.find('.diags').show();
				}
			}
		}); 
		
		return false;
	});

	$("input[name=close]").click(function(){
		$(this).parents(".import-block").toggle();
		return false;
	});
	
	$("#objects .tags").editable(function(value, settings){
		var self = $(this), parent = self.parents(".object"),  id = parent.length ? parent.attr("id").substr(4) : 0;
		id && $.post("tags-" + id + "/", {value:value, l01:d.l01});
		return value;
	}, {
		submit:"<?=ht('Save', 'tpl')?>",
		indicator: '<img src="img/ajax-loader.gif">',
		placeholder: "<?=ht('None', 'tpl')?>",
		width: "none"
	});
	
});

</script><? endScript();

?><fieldset><legend> <?=dat('page','name')?> </legend><div><form id="objects-form" action="" method="post" enctype="multipart/form-data">

<? if (sizeof($d = dat('objects'))) {

	?><table class="adm dnd filtered" id="objects"><thead><tr class="nodrag nodrop">
	<td>
		<span class="filters">
			<? if ($ui['f'] & COM_ENABLED) { ?>
				<a title="<?=ht('Show unpowered', 'tpl')?>" class="button poweroff" href="pass-<?=COM_ENABLED?>/"></a>
			<? } else { ?>
				<a title="<?=ht('Hide unpowered', 'tpl')?>" class="button poweron" href="stop-<?=COM_ENABLED?>/"></a>
			<? } ?>
			<? if ($ui['f'] & COM_VISIBLE) { ?>
				<a title="<?=ht('Show hidden', 'tpl')?>" class="button hidden" href="pass-<?=COM_VISIBLE?>/"></a>
			<? } else { ?>
				<a title="<?=ht('Hide hidden', 'tpl')?>" class="button visible" href="stop-<?=COM_VISIBLE?>/"></a>
			<? } ?>
			<? if ($ui['f'] & COM_GLOBAL) { ?>
				<a title="<?=ht('Show global', 'tpl')?>" class="button globaloff" href="pass-<?=COM_GLOBAL?>/"></a>
			<? } else { ?>
				<a title="<?=ht('Hide global', 'tpl')?>" class="button globalon" href="stop-<?=COM_GLOBAL?>/"></a>
			<? } ?>
		</span>
		<? if ($p['configure']) { ?><?=t('Edit', 'tpl')?><? } ?>
		
	</td><? if ($p['configure']) { ?><td></td><? } ?>
	<td><?=t('Name', 'tpl')?></td><td><?=t('Type', 'tpl')?></td><td><?=t('Tag', 'tpl')?></td></tr></thead><tbody><?

	foreach($d as $a) {
		?><tr id="obj-<?=$a[id]?>" class="object">
			<td><?
				if ($p['configure']) {
					
					if ($a['attr'] & COM_ENABLED) {
						?><a title="<?=ht('Disable', 'tpl')?> «<?=h($a['name'])?>»" class="button poweron" href="disable-<?=$a[id]?>/"></a><?
					} else {
						?><a title="<?=ht('Enable', 'tpl')?> «<?=h($a['name'])?>»" class="button poweroff" href="enable-<?=$a[id]?>/"></a><?
					}
			
					if ($a['attr'] & COM_VISIBLE) {
						?><a title="<?=ht('Hide', 'tpl')?> «<?=h($a['name'])?>»" class="button visible" href="hide-<?=$a[id]?>/"></a><?
					} else {
						?><a title="<?=ht('Show', 'tpl')?> «<?=h($a['name'])?>»" class="button hidden" href="show-<?=$a[id]?>/"></a><?
					}
				
					if ($a['id_parent']) {
						?><a title="<?=ht('Make «@name» global', 'tpl', array('@name' => $a['name']))?>" class="button globaloff" href="globalize-<?=$a[id]?>/"></a><?
					} else {
						?><a title="<?=ht('Make «@name» local', 'tpl', array('@name' => $a['name']))?>" class="button globalon" href="localize-<?=$a[id]?>/"></a><?
					}
					
				} else {
					if ($a['attr'] & COM_ENABLED) {
						?><div class="button poweron"></div><?
					} else {
						?><div class="button poweroff"></div><?
					}
			
					if ($a['attr'] & COM_VISIBLE) {
						?><div class="button visible"></div><?
					} else {
						?><div class="button hidden"></div><?
					}
				
					if ($a['id_parent']) {
						?><div class="button globaloff"></div><?
					} else {
						?><div class="button globalon"></div><?
					}
				}

				if (($a['id_parent'] && $p['edit']) || $p['configure']) {
					?><a title="<?=ht('Edit «@name» data', 'tpl', array('@name' => $a['name']))?>" class="button edit" href="component-<?=$a[id]?>/"></a><?
				}
				
				if ($p['configure']) {
					?><a title="<?=ht('Edit «@name» settings', 'tpl', array('@name' => $a['name']))?>" class="button settings" href="settings-<?=$a[id]?>/"></a><?
				}

				if ($p['configure']) {

					if ($a['export']) {
						?><a title="<?=ht('Export «@name» data', 'tpl', array('@name' => $a['name']))?>" class="button export" href="export-<?=$a[id]?>/"></a><?
					} else {
						?><div class="button exportdis"></div><?
					}

					if ($a['import']) {
						?><a title="<?=ht('Import «@name» data', 'tpl', array('@name' => $a['name']))?>" class="button import" href="import-<?=$a[id]?>/"></a>
						<div class="import-block">
							<div class="form">
								<label><?=ht('Choose the component data file', 'tpl')?><br /><input type="file" name="import[<?=$a['id']?>]"></label>
								<input type="submit" name="import_objects" value="<?=ht('Upload', 'tpl')?>">
								<input type="button" name="close" value="<?=ht('Close', 'tpl')?>">
							</div>
							<div class="diags">
								<div></div>
								<input type="button" name="close" value="<?=ht('Close', 'tpl')?>">
							</div>
						</div><?
					} else {
						?><div class="button importdis"></div><?
					}
					
					
					?><a title="<?=ht('Kill', 'tpl')?> «<?=h($a['name'])?>»" class="button kill" href="delete-<?=$a[id]?>/"></a><?
				}

			?></td>
			
			<? if ($p['configure']) { ?><td class="dragHandle"></td><? } ?>
			
			<td><?
				if (($a['id_parent'] && $p['edit']) || $p['configure']) {
					?><a href="component-<?=$a['id']?>/" title="<?=ht('Edit «@name» data', 'tpl', array('@name' => $a['name']))?>"><?=$a['name']?></a><?
				} else {
					?><?=$a['name']?><?
				}
			?></td>
			<td><?
				if ($p['configure']) {
					?><a href="settings-<?=$a['id']?>/" title="<?=ht('Edit «@name» settings', 'tpl', array('@name' => $a['name']))?>"><?=isset($DATA['cpn_list'][$a['value']]) ? $DATA['cpn_list'][$a['value']]['name'] : $a['value']?></a><?
				} else {
					?><?=isset($DATA['cpn_list'][$a['value']]) ? $DATA['cpn_list'][$a['value']]['name'] : $a['value']?><?
				}
			?></td>
			<td><?
				if ($p['configure']) {
					?><span class="tags edit"><?=$a['text']?></span><?
				} else {
					?><?=$a['text']?><?
				}
			?></td>
		  </tr>
		<?
	}

	?></tbody></table><?
	


} else {

	?><?=t('No components yet', 'tpl')?><?

} ?>

</form>

<? if ($p['configure'] && $d = dat('components')) { ?>
	<h3><?=t('Add component', 'tpl')?></h3>
	<form action="" method="post" id="create-form">
		<label><?=t('Name', 'tpl')?>
			<input type="text" name="name" /></label>
	
		<label><?=t('Type', 'tpl')?>
			<select name="value"><?
				foreach ($d as $o => $c) { // processing groups of components
					?><optgroup label="<?=dat('group_list', $o)?>"><?
						foreach ($c as $v) { // processing components
							?><option value="<?=$v['path']?>"><?=$v['name']?></option><?
						} 
					?></optgroup><?
				}
			?></select>
		</label>
			
		<? foreach (dat('cpn_list') as $v) { 
			?><span id="com-create-<?=str_replace(' ', '-', $v['path'])?>" class="descr hidden"><?=$v['description']?></span><?
		} ?>
	
		<p><input type="submit" name="create_object" value="<?=t('Add', 'tpl')?>" /></p>
	</form>
<? } ?>

</div></fieldset>


<? include block('_htmla');